package com.tarena.lbs.controller.web;

import com.tarena.lbs.dto.FileUploadResult;
import com.tarena.lbs.service.FileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/web/attach")
@Api(tags = "文件上传接口")
public class FileUploadController {

    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件", notes = "上传文件并返回上传结果")
    public FileUploadResult uploadFile(
            @ApiParam(name = "file", value = "要上传的文件", required = true)
            @RequestParam("file") MultipartFile file,
            @ApiParam(name = "userId", value = "用户 ID", required = true)
            @RequestParam Long userId,
            @ApiParam(name = "userName", value = "用户姓名", required = true)
            @RequestParam String userName) {
        return fileStorageService.uploadFile(file, userId, userName);
    }

    @DeleteMapping("/{fileUuid}")
    @ApiOperation(value = "删除文件", notes = "根据文件 UUID 删除文件")
    public boolean deleteFile(@PathVariable String fileUuid) {
        return fileStorageService.deleteFile(fileUuid);
    }
}